package com.gewu.pm.ui.adapter.funds;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.GoldBeanApprovalBean;

/**
 * 金豆活动审批流程适配器
 */
public class GoldBeanApprovalAdapter extends AppAdapter<GoldBeanApprovalBean> {

    public GoldBeanApprovalAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ApprovalViewHolder();
    }

    private final class ApprovalViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView mStatusIconView;
        private final View mLineView;
        private final TextView mStepTitleView;
        private final ImageView mAvatarView;
        private final TextView mApproverInfoView;
        private final TextView mTimeView;

        private ApprovalViewHolder() {
            super(R.layout.approval_flow_item);
            mStatusIconView = findViewById(R.id.iv_status_icon);
            mLineView = findViewById(R.id.view_line);
            mStepTitleView = findViewById(R.id.tv_step_title);
            mAvatarView = findViewById(R.id.iv_avatar);
            mApproverInfoView = findViewById(R.id.tv_approver_info);
            mTimeView = findViewById(R.id.tv_time);
        }

        @Override
        public void onBindView(int position) {
            GoldBeanApprovalBean item = getItem(position);
            
            // 设置时间和内容
            mTimeView.setText(item.getTime());
            mApproverInfoView.setText(item.getContent());
            
            // 第一个是"发起人"，其他是"审批人"
            if (position == 0) {
                mStepTitleView.setText("发起人");
            } else {
                mStepTitleView.setText("审批人");
            }
            
            // 获取节点图标的布局参数
            ViewGroup.LayoutParams iconParams = mStatusIconView.getLayoutParams();
            
            // 根据状态设置节点图标、大小和竖线颜色
            // 0=待审批，1=已通过，2=已驳回
            switch (item.getStatus()) {
                case 1:
                    // 已通过：绿色勾（15dp）
                    mStatusIconView.setImageResource(R.drawable.icon_complete_green);
                    iconParams.width = getResources().getDimensionPixelSize(R.dimen.dp_15);
                    iconParams.height = getResources().getDimensionPixelSize(R.dimen.dp_15);
                    mStatusIconView.setLayoutParams(iconParams);
                    mLineView.setBackgroundColor(getColor(R.color.green_label));
                    break;
                case 0:
                    // 待审批：灰色圆点（8dp）
                    mStatusIconView.setImageResource(R.drawable.timeline_dot_grey);
                    iconParams.width = getResources().getDimensionPixelSize(R.dimen.dp_8);
                    iconParams.height = getResources().getDimensionPixelSize(R.dimen.dp_8);
                    mStatusIconView.setLayoutParams(iconParams);
                    mLineView.setBackgroundColor(getColor(R.color.grey_01));
                    break;
                case 2:
                    // 已驳回：红色圆点（8dp）
                    mStatusIconView.setImageResource(R.drawable.icon_reject_red);
                    iconParams.width = getResources().getDimensionPixelSize(R.dimen.dp_8);
                    iconParams.height = getResources().getDimensionPixelSize(R.dimen.dp_8);
                    mStatusIconView.setLayoutParams(iconParams);
                    mLineView.setBackgroundColor(getColor(R.color.grey_01));
                    break;
            }
            
            // 如果是最后一个 item，隐藏竖线
            if (position == getItemCount() - 1) {
                mLineView.setVisibility(View.INVISIBLE);
            } else {
                mLineView.setVisibility(View.VISIBLE);
            }
        }
    }
}

